package ar.com.fiuba.domain.impl;

import static java.util.Collections.sort;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import ar.com.fiuba.application.api.NgrammDao;
import ar.com.fiuba.domain.Ngramm;
import ar.com.fiuba.domain.Word;
import ar.com.fiuba.domain.api.Dictionary;

public class DictionaryImpl implements Dictionary {

	private NgrammDao ngrammDao;

	public DictionaryImpl(NgrammDao ngrammDao) {
		this.ngrammDao = ngrammDao;
	}

	@Transactional
	public List<Word> getWordsWithSameNGramms(Set<Ngramm> ngramms) {
		
		List<Word> words = new ArrayList<Word>(); 
		
		for (Ngramm ngramm : ngramms) {
			
			Ngramm myNgramm = ngrammDao.findByValue(ngramm.getValue());
		
			for (Word word : myNgramm.getWords()) {
				if (!words.contains(word)){
					words.add(word);
				}
			}
		}
		sort(words);
		return words;
	}

}
